package lab.gene.service.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
//import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
//import java.io.OutputStreamWriter;
import java.util.List;

import lab.gene.pojo.Gene;
import lab.gene.dao.GeneDAO;
import lab.gene.pojo.PrimerSequence;
import lab.gene.dao.PrimerSequenceDAO;
import lab.gene.service.PrimerSequenceService;

public class PrimerSequenceServiceImpl implements PrimerSequenceService {

	@Override
	public List<PrimerSequence> getAllPrimerSequencesByGeneName(String geneName) {
		// TODO Auto-generated method stub
		List<PrimerSequence> primersequences = null;
		PrimerSequenceDAO dao = new PrimerSequenceDAO();
		primersequences = dao.getAllPrimerSequencesByGeneName(geneName);
		return primersequences;
	}
	
	@Override
	public boolean updateSequence(File file)  {
		//open the file and read the line
		String line = null;
		String [] rows = null;
		String geneName = null;
		String primer = null;
		String plate = null;
		String well = null;
		String seq = null;
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
			//OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("C:\\Users\\tom\\Dropbox\\LabMaintenance\\seq.txt"));
			try {
				if ((line = br.readLine()) != null) {
					//System.out.println(line);
					//check the file company
					rows = line.split(",");
					if (rows.length == 19) {
						//System.out.println(rows.length);
						
						while (((line = br.readLine()) != null)&&(!line.contains(",,,,,"))) {
							rows = line.split(",");
							if (!rows[0].contains("REMAINDER")) {
								plate = rows[0].replace(".", "");
								well = rows[5];
								seq = rows[7];
								primer= rows[6].replace("`", "").substring(0, 3);
								geneName = "TK"+rows[6].replace("`", "").substring(3, 7);
								System.out.println("plate="+plate+" well="+well+" seq="+seq+" primer="+primer+" geneName="+geneName);
								//connect to db and save it
								PrimerSequenceDAO dao = new PrimerSequenceDAO();
								PrimerSequence pSeq = dao.getPrimerSequenceByName(geneName, primer);
								if (pSeq == null) {
									//first search if exists the data
									//if not save it directly
									//System.out.println("Primer Sequence Not Found, going to add");
									pSeq  = new PrimerSequence();
									GeneDAO gdao = new GeneDAO();
									Gene gene = gdao.getGene(geneName);
									if(gene != null) {
									pSeq.setGene(gene);
									pSeq.setPlate(plate);
									pSeq.setPrimer(primer);
									pSeq.setSequence(seq);
									pSeq.setWell(well);
									dao.addPrimerSequence(pSeq);
									}
								}
								else {
									//else update plate and well
									if ((!well.equals(pSeq.getWell()))||(!plate.equals(pSeq.getPlate()))) {
										int id = pSeq.getId();
										//System.out.println("Updating*******************");
										dao.updatePrimerSequence(id, primer, well, plate);
									}
								}
							}
						}
					}
					else
						if (rows.length == 3){
							//System.out.println("B company");
							while((line = br.readLine()) != null) {
								rows = line.split(",");
								//System.out.println(line);
								if (rows[0].contains("Plate")) {
									plate = rows[0].replace("Plate ", "").replace(".", "");
								}
								else 
									if ((rows.length==3)&&(rows[2]!=""&&rows[1]!="")) {
										well = rows[0];
										seq = rows[2];
										rows[1] = rows[1].replace("`", "");
										primer = rows[1].substring(0, 3);
										geneName = "TK"+rows[1].substring(3, 7);
										//System.out.println("gene="+gene+" primer="+primer+" plate="+plate+" well="+well+" seq="+seq);
										PrimerSequenceDAO dao = new PrimerSequenceDAO();
										PrimerSequence pSeq = dao.getPrimerSequenceByName(geneName, primer);
										if (pSeq == null) {
											//first search if exists the data
											//if not save it directly
											//System.out.println("Primer Sequence Not Found, going to add");
											pSeq  = new PrimerSequence();
											GeneDAO gdao = new GeneDAO();
											Gene gene = gdao.getGene(geneName);
											if(gene != null) {
												pSeq.setGene(gene);
												pSeq.setPlate(plate);
												pSeq.setPrimer(primer);
												pSeq.setSequence(seq);
												pSeq.setWell(well);
												dao.addPrimerSequence(pSeq);
											}
										}
										else {
											//else update plate and well
											if ((!well.equals(pSeq.getWell()))||(!plate.equals(pSeq.getPlate()))) {
												int id = pSeq.getId();
												//System.out.println("Updating*******************");
												dao.updatePrimerSequence(id, primer, well, plate);
											}
										}
									}
									else
										break;
	
							}
						}
					
				}
				br.close();
			}	
			catch(IOException ioe) {
				ioe.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return true;
	}
	
	@Override
	public boolean addPrimer(String geneName, String primer, String plate, String well, String sequence) {
		PrimerSequenceDAO dao = new PrimerSequenceDAO();
		PrimerSequence s = dao.getPrimerSequenceByName(geneName, primer);
		if(s == null) {
			PrimerSequence seq = new PrimerSequence();
			GeneDAO gdao = new GeneDAO();
			Gene gene = gdao.getGene(geneName);
			seq.setGene(gene);
			seq.setPlate(plate);
			seq.setPrimer(primer);
			seq.setSequence(sequence);
			seq.setWell(well);
			dao.addPrimerSequence(seq);
			return true;
		}
		else
			return false;
	}
	@Override
	public void deletePrimer(Integer id) {
		PrimerSequenceDAO dao = new PrimerSequenceDAO();
		dao.deletePrimerSequencebyId(id);
	}

}
